<?php
/**
 * Created by PhpStorm.
 * User: Silence
 * Date: 2017-07-12
 * Time: 21:17
 */

namespace App\Http\Controllers\Admin;

use App\Base\BaseFunc;
use App\Base\Power;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Input;

class AreaManageController extends Controller
{
    public function areaManage(Power $power)
    {
        $areaData['areaData'] = DB::table('employee')
            //->where('employee_area','=',$area_id)
            ->where('employee_power','=',$power->yuangong())
            ->get();
        $area['area'] = DB::table('area')->get();
        return view("admin.areaManage",$area,$areaData);
    }

    public function areaManages(Power $power,$area_id)
    {
        $area['nowArea'] = DB::table('area')
            ->where('area_id','=',$area_id)->first(); //获得当前区域
        $areaData['areaData'] = DB::table('employee')
            ->where('employee_state','=',1)
            ->where('employee_area','=',$area_id)
            ->where('employee_power','=',$power->yuangong())
            ->get();  //获得当前区域普通员工
        return view("admin.areaManages",$area,$areaData);
    }

    public function addArea(BaseFunc $baseFunc)
    {
        $input = Input::only('area_name','area_info');
        $input['area_time'] = date("Y-m-d H:i:s");
        $newArea = DB::table('area')->where('area_name','=',$input['area_name']) ->get();
        if($newArea)
        {
            $baseFunc->setRedirectMessage(false,"已有此区域，请重新添加",NULL,"/areaManage");
        }
        else
        {
            $data = DB::table('area')->insert($input);
            if($data)
            {
                $baseFunc->setRedirectMessage(true,"添加区域成功",NULL,"/areaManage");
            }
            else
            {
                $baseFunc->setRedirectMessage(false,"添加区域成失败，请重新添加",NULL,"/areaManage");
            }
        }
    }

    public function upArea(Power $power,BaseFunc $baseFunc)
    {
        $areaID = Input::only('area_id'); //获取当前区域ID

        $inputEmployee = Input::only('area_manage'); //获取员工ID

        if($inputEmployee['area_manage'] == NULL)
        {
            $baseFunc->setRedirectMessage(false,"请选择一名员工作为经理",NULL,NULL);
            return redirect()->back();
        }
        else
        {
            $employeeData = DB::table('employee')
                ->where('employee_state','=',1)
                ->where('employee_id', '=', $inputEmployee['area_manage'])
                ->first();
            $inputArea['area_manage'] = $employeeData->employee_name; //获取区域部门经理的姓名

            $powerData = DB::table('employee')
                ->where('employee_state','=',1)
                ->where('employee_area','=',$areaID['area_id'])
                ->where('employee_power','=',$power->jingli())
                ->first(); //检测此区域原部门经理

            if($powerData)
            {
                if($powerData->employee_id != $inputEmployee['area_manage'])
                {
                    $yuangong['employee_power'] = $power->yuangong();
                    DB::table('employee')
                        ->where('employee_state','=',1)
                        ->where('employee_area','=',$areaID['area_id'])
                        ->where('employee_power','=',$power->jingli())
                        ->update($yuangong); //将此区域原经理降级为员工
                }
                $newPower['employee_power'] = $power->jingli();
                DB::table('employee')
                    ->where('employee_state','=',1)
                    ->where('employee_id','=',$inputEmployee['area_manage'])
                    ->update($newPower);//将此区域新经理升级为经理
                DB::table('area')->where('area_id','=',$areaID['area_id'])->update($inputArea);
                $baseFunc->setRedirectMessage(true,"修改此区域信息成功",NULL,NULL);
                return redirect()->back();
            }
            else
            {
                $newPower['employee_power'] = $power->jingli();
                DB::table('employee')
                    ->where('employee_state','=',1)
                    ->where('employee_id','=',$inputEmployee['area_manage'])
                    ->update($newPower);//将此区域新经理升级为经理
                DB::table('area')->where('area_id','=',$areaID['area_id'])->update($inputArea);
                $baseFunc->setRedirectMessage(true,"修改此区域信息成功",NULL,NULL);
                return redirect()->back();
            }
        }
    }

    public function delArea(BaseFunc $baseFunc)
    {
        $input['area_id'] = Input::only('area_id');
        $data = DB::table('area')
            ->where('area_id','=',$input['area_id'])
            ->delete();
        if($data)
        {
            $baseFunc->setRedirectMessage(true,"删除区域成功！",NULL,"/areaManage");
        }
        else
        {
            $baseFunc->setRedirectMessage(false,"删除区域失败！",NULL,"/areaManage");
        }
    }
}